//
//  SearchView.m
//  SolarSimple
//
//  Created by Marcelo Alves on 15/10/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "SearchView.h"


@implementation SearchViewController
@synthesize museekd;
@synthesize currentTicket;

-(void) setCurrentTicket:(NSNumber *)ticket {
  if (currentTicket == ticket) return;

  currentTicket = ticket;  
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ticket.number == %@", ticket];
  [treeController setFetchPredicate:predicate];
}

-(id) init {
  if (![super initWithNibName:@"SearchView" bundle:nil]) return nil;
  
  [self setTitle:@"Search results"];
  
  return self;
}

-(void) awakeFromNib {
  [list setDoubleAction:@selector(downloadFile:)];
  [list setTarget:self];
}

-(BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item {
  return NO;
}

-(IBAction) downloadFile:(id)sender {
  int index = [sender selectedRow];
	if (index < 0) return;
	
	id itemAtRow = [sender itemAtRow:index];
	if (!itemAtRow) return;
	
	NSManagedObject *selectedItem = [itemAtRow representedObject];
  
  [museekd downloadFile:selectedItem];
  
}

-(NSArray *) sortDescriptors {
	if (sidebarSortDescriptors == nil) {
		id userDescriptor = [[NSSortDescriptor alloc] initWithKey: @"user.name"
																										ascending: YES
																										 selector: @selector(localizedCaseInsensitiveCompare:)];

		id fileDescriptor = [[NSSortDescriptor alloc] initWithKey: @"filename"
																										ascending: YES
																										 selector: @selector(localizedCaseInsensitiveCompare:)];
		
		sidebarSortDescriptors = [NSArray arrayWithObjects:userDescriptor, fileDescriptor, nil];
	}
	
	return sidebarSortDescriptors;
}

@end
